package com.answer.service.impl;

import cn.hutool.json.JSONUtil;
import com.answer.mapper.UserAnswerMapper;
import com.answer.model.dto.UserAnswer.UserAnswerAddDTO;
import com.answer.model.dto.UserAnswer.UserAnswerQueryDTO;
import com.answer.model.dto.UserAnswer.UserAnswerUpdateDTO;
import com.answer.model.entity.UserAnswer;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.UserAnswerVo;
import com.answer.scoring.ScoringStrategyContext;
import com.answer.service.UserAnswerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【user_answer(用户答题记录)】的数据库操作Service实现
* @createDate 2024-06-29 15:23:08
*/
@Service
public class UserAnswerServiceImpl extends ServiceImpl<UserAnswerMapper, UserAnswer>
    implements UserAnswerService{

    @Resource
    private ScoringStrategyContext scoringStrategyContext;


    @Override
    public UserAnswer addUserAnswer(UserAnswerAddDTO userAnswerAddDTO) {
        List<String> choices = userAnswerAddDTO.getChoices();
        //用户提交答案进行评分
        UserAnswer userAnswer = scoringStrategyContext.doScore(choices, userAnswerAddDTO.getQuestionId());
        int i = baseMapper.insert(userAnswer);
        if (i==1){
            return userAnswer;
        }
        return null;
    }

    @Override
    public ResultPage<UserAnswerVo> queryAll(UserAnswerQueryDTO userAnswerQueryDTO) {
        Page page = new Page(userAnswerQueryDTO.getCurrent(), userAnswerQueryDTO.getSize());
        baseMapper.selectPage(page, getQueryWrapper(userAnswerQueryDTO));
        List<UserAnswer> userAnswers = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long total = page.getTotal();
        List<UserAnswerVo> userAnswerVoList = new ArrayList<>();
        for (UserAnswer userAnswer : userAnswers) {
            UserAnswerVo userAnswerVo = new UserAnswerVo();
            BeanUtils.copyProperties(userAnswer, userAnswerVo);
            String choices = userAnswer.getChoices();
            userAnswerVo.setChoices(JSONUtil.toList(choices, String.class));
            userAnswerVoList.add(userAnswerVo);
        }

        ResultPage resultPage = new ResultPage();
        resultPage.setRecords(userAnswerVoList);
        resultPage.setCurrent(current);
        resultPage.setPages(pages);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public UserAnswerVo queryOneById(Long id) {

        UserAnswer UserAnswer = baseMapper.selectById(id);
        UserAnswerVo userAnswerVo = new UserAnswerVo();
        BeanUtils.copyProperties(UserAnswer, userAnswerVo);
        String choices = UserAnswer.getChoices();
        userAnswerVo.setChoices(JSONUtil.toList(choices, String.class));
        return userAnswerVo;
    }

    @Override
    public Boolean updateUserAnswer(UserAnswerUpdateDTO userAnswerUpdateDTO) {
        UserAnswer userAnswer = new UserAnswer();
        List<String> choices = userAnswerUpdateDTO.getChoices();
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        BeanUtils.copyProperties(userAnswerUpdateDTO, userAnswer);
        int i = baseMapper.updateById(userAnswer);
        return i == 1;
    }


    private LambdaQueryWrapper<UserAnswer> getQueryWrapper(UserAnswerQueryDTO userAnswerQueryDTO) {
        LambdaQueryWrapper<UserAnswer> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = userAnswerQueryDTO.getUserId();
        return queryWrapper;
    }
    
}




